package com.zte.sys.control;

import com.zte.sys.pojo.*;
import com.zte.sys.service.ISysService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luode
 * @version 1.0
 * @date 2022/10/02 星期日 17:41
 */
@RequestMapping("/sys")
@Controller
public class SysAction {

    @Autowired
    private ISysService sysService;

    public void setSysService(ISysService sysService) {
        this.sysService = sysService;
    }

    public ISysService getSysService() {
        return sysService;
    }


    /**
     * 查询所有用户
     * @return List<Users>
     */
    @RequestMapping("/userList")
    public String userList(HttpSession session, sysUsers users, PageModel<sysUsers> pageModel){
        PageModel<sysUsers> byUser = sysService.getByUser(users, pageModel);
        session.setAttribute("byUser",byUser);
        return "sys/users/userList";
    }

    /**
     * 查询所有部门
     * @return List<Users>
     */
    @RequestMapping("/deptList")
    public String deptList(HttpSession session, sysDept dept, PageModel<sysDept> pageModel){
        PageModel<sysDept> depts = sysService.getDepts(dept, pageModel);
        session.setAttribute("depts",depts);
        return "sys/dept/deptList";
    }


    /**
     * 根据条件查询用户
     * @return
     */
    @RequestMapping("/updateView")
    public String update(String id, Model model, sysDept dept, sysJob job){
        sysUsers user = sysService.queryOne(id);
        List<sysDept> byDept = sysService.getByDept(dept);
        List<sysJob> byJob = sysService.getByJob(job);
        model.addAttribute("user",user);
        model.addAttribute("byDept",byDept);
        model.addAttribute("byJob",byJob);
        return "sys/users/userUpdate";

    }

    /**
     * 添加用户
     * @return
     */
    @RequestMapping("/addSave")
    @ResponseBody
    public void addUser(sysUsers users,Model model,sysDept dept,sysJob job){

        List<sysDept> byDept = sysService.getByDept(dept);
        for (sysDept sysDept : byDept) {
            if (sysDept.getDeptName().equals(users.getDeptName())){
                users.setDeptId(sysDept.getDeptId());
                break;
            }
        }
        List<sysJob> byJob = sysService.getByJob(job);
        for (sysJob sysJob : byJob) {
            if (sysJob.getJobName().equals(users.getJobName())){
                users.setJobId(sysJob.getJobId());
            }
        }
        int i = sysService.addUser(users);
        model.addAttribute("i",i);


    }


    /**
     *根据条件修改用户
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public void updateUser(sysUsers users,Model model,sysDept dept,sysJob job){
        List<sysDept> byDept = sysService.getByDept(dept);
        for (sysDept sysDept : byDept) {
            if (sysDept.getDeptName().equals(users.getDeptName())){
                users.setDeptId(sysDept.getDeptId());
                break;
            }
        }
        List<sysJob> byJob = sysService.getByJob(job);
        for (sysJob sysJob : byJob) {
            if (sysJob.getJobName().equals(users.getJobName())){
                users.setJobId(sysJob.getJobId());
                break;
            }
        }

        if (users.getStatusId().equals("在职")) {
            users.setStatusId("1");
        }else {
            users.setStatusId("2");
        }

        int i = sysService.updateUser(users);
        model.addAttribute("i",i);
    }



    /**
     * 查询部门,职位
     * @return
     */
    @RequestMapping("/querySys")
    public String querySys(sysJob job,sysDept dept, HttpSession session){
        List<sysDept> byDept = sysService.getByDept(dept);
        List<sysJob> byJob = sysService.getByJob(job);
        session.setAttribute("byDept",byDept);
        session.setAttribute("byJob",byJob);
        return "sys/users/userAdd";
    }


    /**
     * 查询省份，城市
     * @return
     */
    @RequestMapping("/queryDept")
    public String queryDept(Model model){
        List<sysProvince> Provinces = sysService.queryProvince();
        List<sysCity> Cities = sysService.queryCity();
        model.addAttribute("Provinces",Provinces);
        model.addAttribute("Cities",Cities);
        return "sys/dept/deptAdd";
    }

    /**
     * 添加部门
     * @param dept
     * @param model
     */
    @RequestMapping("/addDept")
    @ResponseBody
    public void addDept(sysDept dept,Model model){
        List<sysProvince> Provinces = sysService.queryProvince();
        for (sysProvince sysProvince : Provinces) {
            if (sysProvince.getPName().equals(dept.getPName())){
                dept.setPId(sysProvince.getId());
                break;
            }
        }
        List<sysCity> Cities = sysService.queryCity();
        for (sysCity city : Cities) {
            if (city.getCName().equals(dept.getCName())){
                dept.setCId(city.getId());
                break;
            }
        }
        int i = sysService.addDept(dept);
        model.addAttribute("i",i);
    }


    /**
     * 根据条件查询部门
     * @return
     */
    @RequestMapping("/deptView")
    public String deptView(String id, Model model){
        sysDept sysDept = sysService.queryDept(id);
        List<sysProvince> sysProvinces = sysService.queryProvince();
        List<sysCity> sysCities = sysService.queryCity();
        model.addAttribute("sysDept",sysDept);
        model.addAttribute("sysProvinces",sysProvinces);
        model.addAttribute("sysCities",sysCities);
        return "sys/dept/deptUpdate";

    }

    /**
     * 根据部门查询职位
     * @param deptName 部门名称
     */
    @RequestMapping("/getJob")
    @ResponseBody
    public ArrayList<sysJob> getJob(String deptName) {
        return sysService.getJob(deptName);
    }




//    @RequestMapping("/deptList")
//    public String deptList(){
//        return "sys/dept/deptList";
//    }

    @RequestMapping("/positionList")
    public String positionList(){
        return "sys/position/positionList";
    }

    @RequestMapping("/moduleList")
    public String moduleList(){
        return "sys/modules/moduleList";
    }

    @RequestMapping("/logList")
    public String logList(){
        return "sys/logs/logList";
    }

    @RequestMapping("/areaList")
    public String areaList(){
        return "sys/area/areaList";
    }
}
